package com.qlk.odcinterface.manager.impl;

import java.util.List;

import com.qlk.common.manager.BaseManager;
import com.qlk.common.utils.page.PageUtil;
import com.qlk.odcinterface.domain.PmsProductPromotion;
import com.qlk.odcinterface.domain.PmsProductPromotionChannel;
import com.qlk.odcinterface.domain.PmsProductPromotionItem;
import com.qlk.odcinterface.domain.query.PmsProductPromotionQuery;
import com.qlk.odcinterface.dao.PmsProductPromotionChannelDao;
import com.qlk.odcinterface.dao.PmsProductPromotionDao;
import com.qlk.odcinterface.dao.PmsProductPromotionItemDao;
import com.qlk.odcinterface.manager.PmsProductPromotionManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * PmsProductPromotionManager接口的实现类
 * 
 * @author zhaoyanan
 * @version 2016-4-14 13:22:58
 * 
 */
@Component
public class PmsProductPromotionManagerImpl extends BaseManager implements PmsProductPromotionManager {
	
    @Autowired
    private PmsProductPromotionDao pmsProductPromotionDao;
    @Autowired
    private PmsProductPromotionChannelDao pmsProductPromotionChannelDao;
    @Autowired
    private PmsProductPromotionItemDao pmsProductPromotionItemDao;
    /**
     * {@inheritDoc}
     */
    public boolean insert(final List<PmsProductPromotion> pmsProductPromotionList) {
        boolean resultFlag = false;
        if (null != pmsProductPromotionList && pmsProductPromotionList.size() > 0) {
            for (PmsProductPromotion pmsProductPromotion : pmsProductPromotionList) {
                resultFlag = pmsProductPromotionDao.insert(pmsProductPromotion);
                if (!resultFlag) {
                    throw new RuntimeException("批量新增表信息异常");
                }
            }
        }

        return resultFlag;
    }

    /**
     * {@inheritDoc}
     */
    public boolean insert(PmsProductPromotion pmsProductPromotion) {
        return pmsProductPromotionDao.insert(pmsProductPromotion);
    }

    /**
     * {@inheritDoc}
     */
    public boolean update(final PmsProductPromotion pmsProductPromotion) {
        return pmsProductPromotionDao.update(pmsProductPromotion);
    }

    /**
     * {@inheritDoc}
     */
    public List<PmsProductPromotion> queryPmsProductPromotionList(PmsProductPromotionQuery queryBean) {
        return pmsProductPromotionDao.queryPmsProductPromotionList(queryBean);
    }

    /**
     * {@inheritDoc}
     */
    public List<PmsProductPromotion> queryPmsProductPromotionListWithPage(PmsProductPromotionQuery queryBean,
            PageUtil pageUtil) {
        if (null == queryBean) {
            queryBean = new PmsProductPromotionQuery();
        }

        // 查询总数
        int totalItem = queryPmsProductPromotionCount(queryBean);

        if (pageUtil == null) {
            pageUtil = new PageUtil();
        }
        pageUtil.setTotalRow(totalItem);
        pageUtil.init();
        
        if (totalItem > 0) {
            queryBean.setPageIndex(pageUtil.getCurPage());
            queryBean.setPageSize(pageUtil.getPageSize());
            // 调用Dao翻页方法
            return pmsProductPromotionDao.queryPmsProductPromotionListWithPage(queryBean);
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public int queryPmsProductPromotionCount(PmsProductPromotionQuery queryBean) {
        return pmsProductPromotionDao.queryPmsProductPromotionCount(queryBean);
    }

    /**
     * {@inheritDoc}
     */
    public boolean delete(PmsProductPromotion pmsProductPromotion) {
        return pmsProductPromotionDao.delete(pmsProductPromotion);
    }

    /**
     * {@inheritDoc}
     */
    public PmsProductPromotion getPmsProductPromotionById(Integer id) {
        return pmsProductPromotionDao.getPmsProductPromotionById(id);
    }

    /**
     * {@inheritDoc}
     */
    public boolean delete(final PmsProductPromotion[] pmsProductPromotions) {
        boolean resultFlag = false;
        if (null != pmsProductPromotions && pmsProductPromotions.length > 0) {
            for (int i = 0; i < pmsProductPromotions.length; i++) {
                resultFlag = delete(pmsProductPromotions[i]);
                if (!resultFlag) {
                    throw new RuntimeException("批量删除表信息异常!");
                }
            }
        }

        return resultFlag;
    }

    /**
     * {@inheritDoc}
     */
    public boolean exist(PmsProductPromotion pmsProductPromotion) {
        return pmsProductPromotionDao.exist(pmsProductPromotion);
    }
    

    public boolean insertPmsProductPromotion(PmsProductPromotion pmsProductPromotion,List<PmsProductPromotionChannel> channelList,List<PmsProductPromotionItem> itemList) {
       
      //1.11插入单品促销表 TODO 需要换成解析出来的促销表数据
        boolean resultFlag = pmsProductPromotionDao.insert(pmsProductPromotion);
        if(!resultFlag) throw new RuntimeException("单品促销"+pmsProductPromotion.getId()+"主表信息插入失败！");
        //1.12插入单品促销渠道对应表 TODO 需要换成解析出来的促销渠道对应表数据
		if(channelList != null && channelList.size() > 0){
			for(PmsProductPromotionChannel channel : channelList){
				resultFlag = pmsProductPromotionChannelDao.insert(channel);
        		if(!resultFlag) throw new RuntimeException("单品促销"+pmsProductPromotion.getId()+"渠道信息插入失败！");
			}
			
		}
    	//1.13插入单品促销明细表 TODO 需要换成解析出来的促销明细表数据
        if(itemList != null && itemList.size() > 0){
        	for(PmsProductPromotionItem item : itemList){
        		resultFlag = pmsProductPromotionItemDao.insert(item);
        		if(!resultFlag) throw new RuntimeException("单品促销"+pmsProductPromotion.getId()+"渠道信息插入失败！");
        	}
	        
        }
        return resultFlag;
    }
    
    public boolean updatePmsProductPromotion(PmsProductPromotion pmsProductPromotion,List<PmsProductPromotionChannel> channelList,List<PmsProductPromotionItem> itemList) {
    	//2.21修改促销表数据
		boolean resultFlag = pmsProductPromotionDao.update(pmsProductPromotion);   
		//2.22物理删除促销渠道对应表对应的所有渠道数据
		PmsProductPromotionChannel pmsProductPromotionChannel = new PmsProductPromotionChannel();
		pmsProductPromotionChannel.setPromotionId(pmsProductPromotion.getId());//获取单品活动id
		pmsProductPromotionChannelDao.deletePmsProductPromotionChannel(pmsProductPromotionChannel);
		//2.23物理删除明细表对应的全部商品
		PmsProductPromotionItem pmsProductPromotionItem = new PmsProductPromotionItem();
		pmsProductPromotionItem.setPromotionId(pmsProductPromotion.getId());
		pmsProductPromotionItemDao.deletePmsProductPromotionItem(pmsProductPromotionItem);
		//2.24插入促销渠道对应表对应的所有渠道数据 TODO 需要换成解析出来的促销渠道对应表数据
		if(channelList != null && channelList.size() > 0){
			for(PmsProductPromotionChannel channel : channelList){
				resultFlag = pmsProductPromotionChannelDao.insert(channel);
        		if(!resultFlag) throw new RuntimeException("单品促销"+pmsProductPromotion.getId()+"渠道信息插入失败！");
			}
			
		}
		//2.25插入明细表对应的全部商品 TODO 需要换成解析出来的促销明细表数据
		if(itemList != null && itemList.size() > 0){
	        	for(PmsProductPromotionItem item : itemList){
	        		resultFlag = pmsProductPromotionItemDao.insert(item);
	        		if(!resultFlag) throw new RuntimeException("单品促销"+pmsProductPromotion.getId()+"渠道信息插入失败！");
	        	}
		        
	     }
		 return resultFlag;
    
    }
    public boolean updatePmsProductPromotionStatus(PmsProductPromotion pmsProductPromotion){   
    	boolean resultFlag = pmsProductPromotionDao.update(pmsProductPromotion);   
		if(!resultFlag) throw new RuntimeException("单品促销"+pmsProductPromotion.getId()+"渠道信息插入失败！");
    	return resultFlag;
    }
    
    
}
